package algocasts;

public class P67_SearchInBinarySearchTree {
    class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
    }


    // Time: O(h) Space:O(h)
    public TreeNode searchBST(TreeNode root, int val) {
        if (root == null || val == root.val) return root;
        else if (val < root.val) return searchBST(root.left, val);
        else return searchBST(root.right, val);
    }
    // Time: O(h)  Space: O(1)
    public TreeNode searchBSTIterative(TreeNode root, int val) {
        while (root != null && val != root.val) {
            if (val < root.val) root = root.left;
            else root = root.right;
        }
        return root;
    }
}
